#!/usr/bin/perl

require "./mailadmin-lib.pl";
&ReadParse();

my (%dft_uinfo, $msg_over_batnum);

unless (&qmail_dir_check())
{
    &redirect('maildir_edit.cgi');
}

$msg_over_batnum = &text('msg_over_bataction_num', $config{'bataction_num'});

&header($text{'index_title'}, "", undef, 0, 1);

&get_dftuinfo(\%dft_uinfo);

print "<form name=adduform action='save_create_user.cgi' method=post onsubmit=\'return do_submit();\'>\n";

print "<table class=mode1 border=0 width=100%>\n";

print "<tr><td>$text{'dname'}</td><td><b>$in{'dname'}</b></td></tr>";

print "<tr><td valign=top width=20%>$text{'uname'}</td>\n";

# adduser rmuser button table

print "<td width=10%><select name=unlist class=list size=7 value=\"\"></select></td>\n";
print "<td valign=top>\n";
print "<table>\n";
print "<tr><td>&nbsp;&nbsp;&nbsp;<input type=button class=button1 name=btn_aduname value=\"$text{'btn_aduname'}\" onclick='fun_aduname();'></td></tr>\n";
print "<tr><td>&nbsp;&nbsp;&nbsp;<input type=button class=button1 name=btn_rmuname value=\"$text{'btn_rmuname'}\" onclick='fun_rmuname();'></td></tr>\n";

print "</table>\n";
print "</td></tr>";

print "<tr><td>$text{'password'}</td><td><input name=passwd class=text type=password value=\"$dft_uinfo{'upasswd'}\"></td></tr>\n";
print "<tr><td>$text{'confirm_pw'}</td><td><input name=con_passwd class=text type=password value=\"$dft_uinfo{'upasswd'}\"></td></tr>\n";
printf "<tr><td>$text{'uquota'}</td><td><input name=uquota class=text size=8 value=\"%.2f\"><b> $text{'unit_MB'}</b></td></tr>\n", $dft_uinfo{'uquota'}/1048756;
#print "<tr><td>$text{'mbx_usage'}</td><td>$mbx_usage</td></tr>\n";

print "</table>";

print "<p><input name=dname type=hidden value=\"$in{'dname'}\">";
print "<p><input name=unamecache type=hidden value=\"$in{'unamecache'}\">";

print "<p><input type=submit name=btn_create class=button1 value=\"$text{'btn_create'}\" onclick=\"return create_user();\">";

print "</form>";

print <<EOF;
<script>
function do_submit()
{
    document.forms[0].btn_create.disabled = true;
    document.forms[0].btn_aduname.disabled = true;
    document.forms[0].btn_rmuname.disabled = true;
}


function fun_aduname()
{
    if ( document.adduform.unlist.options.length >= $config{'bataction_num'})
    {
	alert("$msg_over_batnum");
	return false;
    }
    var tmp = prompt("$text{'msg_add_uname'}","");
    if (tmp)
    {
	i = document.adduform.unlist.options.length;
	document.adduform.unlist.options[i] = new Option(tmp,tmp);
    }
    refresh_unamecache();
}

function fun_rmuname()
{
    var idx = document.adduform.unlist.selectedIndex;
    
    if ( idx >= 0)
    {
	document.adduform.unlist.options[idx] = null;
    }
    refresh_unamecache();
}

function refresh_unamecache()
{
    var str ="";
    for (i = 0; i < document.adduform.unlist.options.length; i++)
    {
	if (i < document.adduform.unlist.options.length - 1)
	    str += (document.adduform.unlist.options[i].value + ":");
	else
	    str += document.adduform.unlist.options[i].value;
    }
    document.adduform.unamecache.value = str;
}

function create_user()
{
    if (document.adduform.unamecache.value == "")
    {
	alert("$text{'msg_uname_empty'}");
	return false;
    }

    if (document.adduform.passwd.value != document.adduform.con_passwd.value)
    {
	alert("$text{'msg_conpasswd_diff'}");
	return false;
    }
    
}

</script>
EOF


&footer("list_user.cgi?dname=$in{'dname'}", $text{"back_lsuser"});
